<html>
<head><meta charset="utf-8"><title>Value of &quot;returning ... requires ... borrowed for &#x27;a&quot;? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Value.20of.20.22returning.20.2E.2E.2E.20requires.20.2E.2E.2E.20borrowed.20for.20&#x27;a.22.3F.html">Value of &quot;returning ... requires ... borrowed for &#x27;a&quot;?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="227471933"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Value%20of%20%22returning%20...%20requires%20...%20borrowed%20for%20%27a%22%3F/near/227471933" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Value.20of.20.22returning.20.2E.2E.2E.20requires.20.2E.2E.2E.20borrowed.20for.20&#x27;a.22.3F.html#227471933">(Feb 23 2021 at 17:42)</a>:</h4>
<p>Another question raised during training...</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">Thing</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">a</span>: <span class="kt">i32</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">b</span>: <span class="kt">i32</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Thing</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">get_mut_a</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">a</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">get_mut_b</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">b</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">get_both</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;'</span><span class="na">a</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="p">(</span><span class="o">&amp;'</span><span class="na">a</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;'</span><span class="na">a</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">get_mut_a</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">get_mut_b</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite"><pre><span></span><code>error[E0499]: cannot borrow `*self` as mutable more than once at a time
  --&gt; src/lib.rs:15:17
   |
13 |     fn get_both&lt;&#39;a&gt;(&amp;&#39;a mut self) -&gt; (&amp;&#39;a mut i32, &amp;&#39;a mut i32) {
   |                 -- lifetime `&#39;a` defined here
14 |         let a = self.get_mut_a();
   |                 ---- first mutable borrow occurs here
15 |         let b = self.get_mut_b();
   |                 ^^^^ second mutable borrow occurs here
16 |         (a, b)
   |         ------ returning this value requires that `*self` is borrowed for `&#39;a`
</code></pre></div>



<a name="227471974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Value%20of%20%22returning%20...%20requires%20...%20borrowed%20for%20%27a%22%3F/near/227471974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Value.20of.20.22returning.20.2E.2E.2E.20requires.20.2E.2E.2E.20borrowed.20for.20&#x27;a.22.3F.html#227471974">(Feb 23 2021 at 17:42)</a>:</h4>
<p>What is the value of this part of the error text:</p>
<blockquote>
<p>returning this value requires that <code>*self</code> is borrowed for <code>'a</code></p>
</blockquote>
<p>In similar cases, such as <code>(a, b); todo!()</code>, the error is simpler and doesn't mention <code>'a</code>:</p>
<div class="codehilite"><pre><span></span><code>16 |         (a, b);
   |          - first borrow later used here
</code></pre></div>



<a name="227472213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Value%20of%20%22returning%20...%20requires%20...%20borrowed%20for%20%27a%22%3F/near/227472213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Value.20of.20.22returning.20.2E.2E.2E.20requires.20.2E.2E.2E.20borrowed.20for.20&#x27;a.22.3F.html#227472213">(Feb 23 2021 at 17:44)</a>:</h4>
<p>It indicates lifetime requirements that are different from just using the value at that point</p>



<a name="227472297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Value%20of%20%22returning%20...%20requires%20...%20borrowed%20for%20%27a%22%3F/near/227472297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Value.20of.20.22returning.20.2E.2E.2E.20requires.20.2E.2E.2E.20borrowed.20for.20&#x27;a.22.3F.html#227472297">(Feb 23 2021 at 17:44)</a>:</h4>
<p>but yeah, those aren't really relevant for that specific conflict</p>



<a name="227479765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Value%20of%20%22returning%20...%20requires%20...%20borrowed%20for%20%27a%22%3F/near/227479765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Value.20of.20.22returning.20.2E.2E.2E.20requires.20.2E.2E.2E.20borrowed.20for.20&#x27;a.22.3F.html#227479765">(Feb 23 2021 at 18:30)</a>:</h4>
<p>Do you think it would be worth filing an issue for this case? I know that these types of errors can be touchy to get right.</p>



<a name="227481939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Value%20of%20%22returning%20...%20requires%20...%20borrowed%20for%20%27a%22%3F/near/227481939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Value.20of.20.22returning.20.2E.2E.2E.20requires.20.2E.2E.2E.20borrowed.20for.20&#x27;a.22.3F.html#227481939">(Feb 23 2021 at 18:42)</a>:</h4>
<p>I don't think it would hurt</p>



<a name="227608986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Value%20of%20%22returning%20...%20requires%20...%20borrowed%20for%20%27a%22%3F/near/227608986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Value.20of.20.22returning.20.2E.2E.2E.20requires.20.2E.2E.2E.20borrowed.20for.20&#x27;a.22.3F.html#227608986">(Feb 24 2021 at 14:47)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/82479">https://github.com/rust-lang/rust/issues/82479</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>